What is forwarded?
The 'forwarded' npm package is designed to parse the 'X-Forwarded-For' header from an HTTP request, which is a standard header for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer. This package extracts the IP addresses listed in the header and provides them in an array format, making it easier to handle cases where there are multiple proxies in use.
What are forwarded's main functionalities?
Parse X-Forwarded-For header
This feature allows you to parse the 'X-Forwarded-For' header from an HTTP request object and returns an array of IP addresses that were added by each proxy.
const forwarded = require('forwarded');
const req = { headers: { 'x-forwarded-for': '192.0.2.60, 198.51.100.17' } };
const ips = forwarded(req);
console.log(ips); // ['192.0.2.60', '198.51.100.17']
Other packages similar to forwarded
proxy-addr
Similar to 'forwarded', 'proxy-addr' is used to determine the address of the client that connected to the proxy server. It provides more functionality, such as trusting custom IP address ranges and determining the client's IP address from a list of trusted proxies.
ipware
While 'ipware' is not limited to Node.js and is also available for Python, it offers similar functionality for getting the client's IP address from the 'X-Forwarded-For' header or other headers like 'X-Real-IP'. It also provides features to customize trusted proxies and private IP ranges.
request-ip
The 'request-ip' package is another alternative that retrieves the client's IP address from an HTTP request. It supports both IPv4 and IPv6 addresses and works with various headers, including 'X-Forwarded-For'. It is a simple and straightforward package for getting the client's IP address without additional configuration options.
forwarded
Parse HTTP X-Forwarded-For header
Installation
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install forwarded
API
var forwarded = require('forwarded')
forwarded(req)
var addresses = forwarded(req)
Parse the X-Forwarded-For
header from the request. Returns an array
of the addresses, including the socket address for the req
, in reverse
order (i.e. index 0
is the socket address and the last index is the
furthest address, typically the end-user).
Testing
$ npm test
License
MIT